/*
* $Id: HousingsTable.cpp 93 2010-05-26 01:13:39Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/#include "Common/include.h"
#include "HousingsTable.h"
#include "TableNames.h"
#include "../SqlBinder.h"

CHousingsTable::CHousingsTable()
: CTable(HOUSINGS_TABLE_NAME){}
CHousingsTable::~CHousingsTable(){}

void CHousingsTable::CreateTable(IDatabase* pDatabase){
	std::tstring strRequest(_T("CREATE TABLE IF NOT EXISTS "));
	pDatabase->ExecRequest(strRequest + GetNameTable() + 
						_T("(id_housing INTEGER NOT NULL,")\
						_T("name TEXT NOT NULL,")\
						_T("id_family INTEGER NULL,")\
						_T("type_housing INTEGER NULL,")\
						_T("type_frame INTEGER NULL,")\
						_T("is_delete BOOL NULL,")\
						_T("PRIMARY KEY(id_housing ASC AUTOINCREMENT),")\
						_T("CONSTRAINT fkTypeHousing")\
						_T("	FOREIGN KEY(type_housing)")\
						_T("	REFERENCES type_housing(id_type)")\
						_T("	ON DELETE SET NULL,")\
						_T("CONSTRAINT fkTypeFrame")\
						_T("	FOREIGN KEY(type_frame)")\
						_T("	REFERENCES type_frame(id_type)")\
						_T("	ON DELETE SET NULL,")\
						_T("CONSTRAINT fkHousings")\
						_T("	FOREIGN KEY(id_family)")\
						_T("	REFERENCES families(id_family)")\
						_T("	ON DELETE SET NULL);"));
}
void CHousingsTable::UpdateValue(IDatabase* pDatabase){
	std::tstring strRequest(_T("UPDATE "));
	CSqlBinder binder(strRequest + GetNameTable() + 
					_T(" SET is_delete=?, name=?, type_housing=?, id_family=?, type_frame=? WHERE id_housing=?"));
	binder.SetValue(4, m_content.s_nFrames);
	binder.SetValue(1, m_content.s_strName);
	binder.SetValue(3, m_content.s_nIdFamily);
	binder.SetValue(2, m_content.s_nType);
	binder.SetValue(5, m_content.s_nId);
	binder.SetValue(0, m_content.s_bDelete);
	pDatabase->ExecRequest(binder.GetRequest());
	m_content.Clear();
}
IResult* CHousingsTable::GetValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("SELECT * FROM "));
	strRequest = strRequest + GetNameTable() + _T(" WHERE is_delete=0");
	if(m_content.s_nId){
		binder.SetRequest(strRequest + _T(" AND id_housing=?"));
		binder.SetValue(0, m_content.s_nId);
	}else if(m_content.s_nIdFamily != -1){
		binder.SetRequest(strRequest + _T(" AND id_family=?"));
		binder.SetValue(0, m_content.s_nIdFamily);
	}else
		binder.SetRequest(strRequest);
	m_content.Clear();
	return FillContent(pDatabase->ExecRequest(binder.GetRequest()));
}
void CHousingsTable::SetValue(IDatabase* pDatabase){
	pDatabase->BeginTransaction(_T("SET_HOUSING"));
	CSqlBinder binder;
	std::tstring strRequest(_T("INSERT INTO "));
	binder.SetRequest(strRequest + GetNameTable() + 
						_T("(name, id_family, type_housing, type_frame, is_delete) VALUES(?, ?, ?, ?, 0)"));
	binder.SetValue(0, m_content.s_strName);
	binder.SetValue(1, m_content.s_nIdFamily);
	binder.SetValue(2, m_content.s_nType);
	binder.SetValue(3, m_content.s_nFrames);
	pDatabase->ExecRequest(binder.GetRequest());
	strRequest = _T("SELECT MAX(id_housing) as id_housing FROM ");
	MyTable table = pDatabase->ExecRequest(strRequest + GetNameTable());
	if(table.empty())
		m_content.s_nId = 0;
	else
		m_content.s_nId = _ttoi(table[0][0].m_strValueColumn.c_str());
	pDatabase->EndTransaction(_T("SET_HOUSING"));
}
void CHousingsTable::DeleteValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("UPDATE "));
	if(m_content.s_nId){
		CSqlBinder binder(strRequest + GetNameTable() + 
			_T(" SET is_delete=1 WHERE id_housing=?"));
		binder.SetValue(0, m_content.s_nId);
		pDatabase->ExecRequest(binder.GetRequest());
	}
	m_content.Clear();
}
IOperation::i_content* CHousingsTable::CreateContent(){
	return new content;
}

CHousingsTable::content::content(){
	Clear();
}
void CHousingsTable::content::Clear(){
	s_nId = 0;
	s_strName.clear();
	s_nIdFamily = -1;
	s_nType = 0;
	s_nFrames = 0;
	s_bDelete = false;
}
void CHousingsTable::content::Set(const std::vector<CTableItem>& aContent){
	for(std::vector<CTableItem>::const_iterator it = aContent.begin(); it != aContent.end(); it++){
		if(it->m_strNameColumn == _T("id_housing"))
			s_nId = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("type_housing"))
			s_nType = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("type_frame"))
			s_nFrames = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("id_family"))
			s_nIdFamily = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("name"))
			s_strName = it->m_strValueColumn;
		else if(it->m_strNameColumn == _T("is_delete"))
			s_bDelete = (bool)_ttoi(it->m_strValueColumn.c_str());
	}
}
void CHousingsTable::content::Free(){
	delete this;
}
